#include <stdio.h>
#include <time.h>
#include <assert.h>
#include "include/array_generator.h"
#include "include/array_help.h"


void sort(int arr[], int len) {
    for (int i = 0; i < len; i++) {
        int minIndex = i;
        for (int j = i + 1; j <len; j++) {
            if (arr[j] < arr[minIndex])
                minIndex = j;
        }
        Swap(arr + i, arr + minIndex, sizeof(int)); 
    }   
}

// 通用类型算法
void sortGeneric(void* base, int len, int size, int (*compare)(void*, void*)) {
    for (int i = 0; i < len; i++) {
        // 第 i 和元素的地址
        void* minAddr = (char*)base + (i * size); 
        for (int j = i + 1; j < len; j++) {
            // i， 下一个元素的地址
            void* nextAddr = (char*)base + (j * size); 
            if (compare(nextAddr, minAddr) < 0)
                minAddr = nextAddr; 
        }
        Swap((char*)base + (i * size), minAddr, size); 
    }
}

int int_cmp(void* vp1, void* vp2) {
    return *(int*)vp1 - *(int*)vp2;
}

// > 选择排序
int main(int argc, char *argv[]) {
    int len = 40000;
    int arr[len]; 
    GenRandomArray(arr, len, len); 
    // PrintIntArray(arr, len); 

    time_t start = time(NULL);
    // sort(arr, len);
    sortGeneric(arr, len, sizeof(int), int_cmp); 
    time_t end = time(NULL); 
    printf("Time taken: %f seconds\n", difftime(end, start));
    // PrintIntArray(arr, len); 
    assert(IsSorted(arr, len) == 1);
    return 0; 
}